package com.china08.yxyapi.api.home.activity.lecture;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.statistics.YxyHuodongLectureSchoolStatisticsRepository;

@CrossOrigin
@RestController
public class ActivityLectureIndexController extends YxyApiBaseController {

    public static final String SQL = "SELECT SUM(pack_count) AS num,area_name_district AS NAME,area_id_district AS CODE FROM yxy_huodong.yxy_huodong_lecture_school_statistics WHERE area_id_district!='451381' GROUP BY area_id_district ORDER BY num DESC";

    @Autowired
    private YxyHuodongLectureSchoolStatisticsRepository yxyLectureRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/api_yw/home/activity/tea/lecture/index", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(//
            HttpServletRequest request, //
            HttpServletResponse response//
    ) {
        RespModel respModel = new RespModel();
        Integer countAll = yxyLectureRepository.countAll();
        if (countAll == null) {
            countAll = 0;
        }
        respModel.count = countAll;
        List<Map<String, Object>> list = jdbcTemplate.queryForList(SQL);
        if (CollectionUtils.isNotEmpty(list)) {
            for (Map<String, Object> map : list) {
                respModel.data.add((BigDecimal) map.get("num"));
                respModel.name.add("市辖区".equals(map.get("name")) ? "市直辖" : (String) map.get("name"));
                respModel.codeArea.add((String) map.get("code"));
            }
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<String> name = new ArrayList<String>();
        public List<BigDecimal> data = new ArrayList<>();
        public List<String> codeArea = new ArrayList<>();
        public Integer count = 0;
    }
}
